package infra

import (
	"crypto/rsa"
	"fmt"
	"github.com/casdoor/casdoor-go-sdk/auth"
	"github.com/golang-jwt/jwt/v4"
	"os"
)

type OAuth struct {
	CasdoorEndpoint     string
	ClientId            string
	ClientSecret        string
	CasdoorOrganization string
	CasdoorApplication  string
	JwtPublicKey        string
}

var PublicKey *rsa.PublicKey

func init() {
	CasIP := "http://39.101.140.145:30002"
	Cid := "48a37c1ecd28f2818bca"
	Cst := "6666e6fb141959237cb2d82bd60b5f83e0887ca0"
	Co := "gluenet"
	Ca := "gluenet"

	mode := os.Getenv("MODE")
	if mode == "prod" {
		CasIP = os.Getenv("CASDOOR")
		Cid = os.Getenv("AUTH_CID")
		Cst = os.Getenv("AUTH_CST")
		JPublicKey = os.Getenv("AUTH_JPK")
		Co = os.Getenv("AUTH_ORG")
		Ca = os.Getenv("AUTH_APP")
	}

	var err error
	PublicKey, err = jwt.ParseRSAPublicKeyFromPEM([]byte(JPublicKey))
	if err != nil {
		panic(err)
	}

	auth.InitConfig(CasIP, Cid, Cst, JPublicKey, Co, Ca)
}

func (r *OAuth) Login(code, state string) (*auth.Claims, error) {
	token, err := auth.GetOAuthToken(code, state)
	if err != nil {
		return nil, err
	}
	claims, err := auth.ParseJwtToken(token.AccessToken)
	if err != nil {
		return nil, err
	}
	claims.AccessToken = token.AccessToken
	return claims, nil
}
func (r *OAuth) Auth(token string) (*auth.Claims, error) {
	// method one
	if claim, err := ParseWithClaims(token); err == nil {
		return claim, nil
	}

	// if one is error use method two
	claims, err := auth.ParseJwtToken(token)
	if err != nil {
		return nil, err
	}
	claims.AccessToken = token
	return claims, nil
}

func ParseWithClaims(token string) (*auth.Claims, error) {
	tokenClaim, err := jwt.ParseWithClaims(token, &auth.Claims{}, func(token *jwt.Token) (interface{}, error) {
		if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
		}
		publicKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(JPublicKey))
		if err != nil {
			return nil, err
		}

		return publicKey, nil
	})
	if err != nil {
		return nil, err
	}
	claim, ok := tokenClaim.Claims.(*auth.Claims)
	if !ok {
		return nil, fmt.Errorf("claim is not auth.claims")
	}
	return claim, nil
}

var JPublicKey = `
-----BEGIN CERTIFICATE-----
MIIE3TCCAsWgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMCgxDjAMBgNVBAoTBWFk
bWluMRYwFAYDVQQDEw1jZXJ0LWJ1aWx0LWluMB4XDTIyMDcxNDAxMTEyOVoXDTQy
MDcxNDAxMTEyOVowKDEOMAwGA1UEChMFYWRtaW4xFjAUBgNVBAMTDWNlcnQtYnVp
bHQtaW4wggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDCqNhvmP92bAAF
/uzXPvrHclpGlZdFhCBraVFEU/4MvtSYK/im4lVb5S+azNr0LGy/OY4kVtg80e58
eqM23tMsB301zCJO4W6QgTKOv2NUHphOtHs3HktKZ6L9FUXzt7hjeP952cnpfAZE
mR3Kno4UMfRwkU8gYmPickNNKwTEpqBgHzypZNhn8o/Hk2WM+t/dXFLvh5PQXkAa
goArAeTWph/VX3Dbgu8M2K2AKKy//QyUbEaIbHWbZBBYg9uyVPE1j9moFDYgpBzC
6JZ2CHavyxYc4b9Dt4HcHS+dMIK141fciSZutGsLzzRTpucH7kC1xbHUZFHsL8aw
vYpUearGxK/2BdiCCYWS735QtGfsF2TNNG/jLP4Kc33Eeki7EU1WpPejAxJyFGs+
yxESks5Ct42mPEjoRVQ+keZ3tc6i3r3JquNroLn+PzBwgIzyFJz4i4bLknxQnk2L
gLdfOA2L/7YpReghODSMzN3wX3Zlho6MYQGmZyyzSf2p5N9lcK47nD6Q9w4lajim
R5Ksg9F3eVQcvmxFRwsrMU7jR/6iO8sNVjfqNKr59Casbx2AStgG5Z8HcUlryOjK
kGyLUSMRegl/ye0NoyzqGlSEUO9GGf1diCYTC/Nwug9vq+OV+G6qyGIEPlBC8Ru5
wjp3i40lw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